package com.vincent.baseapp.util;

import android.app.Activity;
import android.content.Context;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;
import android.widget.TextView;
import android.widget.Toast;

import com.vincent.baseapp.BaseApplication;
import com.vincent.baseapp.R;
import com.vincent.baseapp.util.view.LoadingView;

/**
 * Created by 0704 on 2016/6/27.
 */
public class CommonUtil {

    public static void spannable(Context context, String a, String b, TextView textView) {
        SpannableStringBuilder spanStringList = new SpannableStringBuilder();
        SpannableString spanString = new SpannableString(a);
        SpannableString spanString2 = new SpannableString(b);
        spanString.setSpan(new AbsoluteSizeSpan(ScreenUtil.getInstance(context).sp2px(25)), 0, a
                .length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        spanStringList.append(spanString);
        spanStringList.append(spanString2);
        textView.setText(spanStringList);
    }

    public static void spannableOne(Context context, String a, String b, TextView textView) {
        SpannableStringBuilder spanStringList = new SpannableStringBuilder();
        SpannableString spanString = new SpannableString(a);
        SpannableString spanString2 = new SpannableString(b);
        spanString.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color
                .text_main_color)), 0, a.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        spanString2.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color
                .text_second_color)), 0, b.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        spanStringList.append(spanString);
        spanStringList.append(spanString2);
        textView.setText(spanStringList);
    }

    public static void spannableTwo(Context context, String a, String b, TextView textView) {
        SpannableStringBuilder spanStringList = new SpannableStringBuilder();
        SpannableString spanString = new SpannableString(a);
        SpannableString spanString2 = new SpannableString(b);
        spanString.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color
                .text_second_color)), 0, a.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        spanString2.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color
                .text_main_color)), 0, b.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        spanStringList.append(spanString);
        spanStringList.append(spanString2);
        textView.setText(spanStringList);
    }
    /**
     * 显示数据加载框
     *
     * @param context
     */
    public static void showLoadingView(Context context) {

        BaseApplication application = (BaseApplication) ((Activity) context).getApplication();
        LoadingView loadingView = application.getLoadingView();
        if (loadingView != null && loadingView.isShowing()) {
            loadingView.dismiss();
        }
        loadingView = new LoadingView(context);
        loadingView.setCanceledOnTouchOutside(false);
        loadingView.show();
        application.setLoadingView(loadingView);
    }

    /**
     * 隐藏数据加载框
     *
     * @param context
     */
    public static void dismissLoadingView(Context context) {
        BaseApplication application = (BaseApplication) ((Activity) context).getApplication();
        LoadingView loadingView = application.getLoadingView();
        if (loadingView != null && loadingView.isShowing()) {
            loadingView.dismiss();
        }
    }

    public static void showToast(Context context,String content) {
        Toast.makeText(context, content, Toast.LENGTH_LONG).show();
    }
}
